package zhongbo.java.air.dictionary.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.common.util.AssertUtil;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.mapper.AirPortMapper;
import zhongbo.java.air.dictionary.model.AirPort;
import zhongbo.java.air.dictionary.service.IAirPortService;

import java.util.Date;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月01日 星期二 08:10:47
 */
@Service
@Slf4j
public class AirPortServiceImpl implements IAirPortService<AirPort> {

    @Autowired
    private AirPortMapper airPortMapper;

    @Override
    public AirPort add(AirPort airPort) {
        validAirPort(airPort);
        AssertUtil.isNull(airPort.getId(), "实体id不能填写");
        int countByIcao = airPortMapper.selectCountByIcao(airPort.getIcaoCode());
        if (countByIcao > 0) {
            throw new RuntimeException("ICAO码重复");
        }
        int countByIata = airPortMapper.selectCountByIata(airPort.getIataCode());
        if (countByIata > 0) {
            throw new RuntimeException("IATA码重复");
        }

        airPort.setCreateTime(new Date());
        airPort.setUpdateTime(new Date());
        int insert = airPortMapper.insert(airPort);
        if (insert > 0) {
            return airPort;
        }
        throw new RuntimeException("新增实体失败");
    }

    /**
     * 通用验证实体信息
     */
    private void validAirPort(AirPort airPort) {
        if (airPort == null) {
            throw new RuntimeException("实体信息不能为空");
        }
        AssertUtil.maxLength(airPort.getIcaoCode(), 4, "ICAO码不能超过4位");
        AssertUtil.notBlank(airPort.getIcaoCode(), "ICAO码不能为空");
        AssertUtil.maxLength(airPort.getIataCode(), 3, "IATA码不能超过3位");
        AssertUtil.notBlank(airPort.getIataCode(), "IATA码不能为空");
    }

    @Override
    public String delete(String airPortId) {
        if (StrUtil.isBlank(airPortId)) {
            throw new RuntimeException("实体id不能为空");
        }
        int delete = airPortMapper.deleteById(airPortId);
        if (delete > 0) {
            return airPortId;
        }
        throw new RuntimeException("删除实体失败");
    }

    @Override
    public AirPort edit(AirPort edit) {
        validAirPort(edit);
        String airPortId = edit.getId();
        if (StrUtil.isBlank(airPortId)) {
            throw new RuntimeException("实体id不能为空");
        }
        AirPort one = findOne(airPortId);
        if (one == null) {
            throw new RuntimeException("实体不存在");
        }
        edit.setUpdateTime(new Date());
        int update = airPortMapper.updateById(edit);
        if (update > 0) {
            return edit;
        }
        throw new RuntimeException("修改实体失败");
    }

    @Override
    public AirPort findOne(String airPortId) {
        if (StrUtil.isBlank(airPortId)) {
            throw new RuntimeException("实体id不能为空");
        }
        return airPortMapper.selectById(airPortId);
    }

    @Override
    public PageVo<AirPort> findPage(Integer page, Integer pageSize, String keyword, String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<AirPort> queryWrapper = new QueryWrapper<>();
        // 检测是否有排序
        if (StrUtil.isNotBlank(oderByField)) {
            // 检测是正序还是倒序
            boolean isOrderByAsc = Boolean.TRUE.equals(orderByAsc);
            if (isOrderByAsc) {
                // 结果正序排列
                queryWrapper.orderByAsc(StrUtil.toUnderlineCase(oderByField));
            } else {
                // 否则结果倒序排列
                queryWrapper.orderByDesc(StrUtil.toUnderlineCase(oderByField));
            }
        }
        // 支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        Page<AirPort> pageParam = new Page<>(page, pageSize);
        Page<AirPort> airPortPage = airPortMapper.selectPage(pageParam, queryWrapper);

        return new PageVo<>(airPortPage);
    }

    private void validPassWord(String passWord) {
        if (StrUtil.isBlank(passWord)) {
            throw new RuntimeException("密码不能为空");
        }
    }

    private void validAirPortName(String airPortName) {
        if (StrUtil.isBlank(airPortName)) {
            throw new RuntimeException("实体名不能为空");
        }
    }


}
